package se.kth.fericss.quicksort;
public class Check {
	/**
	 * Check for special cases such as already sorted,
	 * an inversed list or all valeus the same
	 * Also Sorts the list in the special cases
	 * @param v Vector to be checked
	 * @param low Start of vector
	 * @param high End of vector
	 * @return True if it had a special case
	 */
	public boolean checkSpecial(int[] v, int low, int high){
		if(checkSorted(v))
			return true;
		if(checkUnSorted(v)){
			inverse(v);
			return true;
		}
		return false;
	}
	/**
	 * Checks if the entire vector is sorted
	 * @param v The vector to check
	 * @return True if sorted
	 */
	private boolean checkSorted(int[] v){
		int n = v.length;
		int i;
		for(i=1;i<n;i++){
			if(v[i-1]>v[i])
				return false;
		}
		return true;
	}
	/**
	 * Checks if the entire vector is inversed sorted
	 * @param v The vector to check
	 * @return True if inversed sorted
	 */
	private boolean checkUnSorted(int[] v){
		int n = v.length;
		int i;
		for(i=1;i<n;i++){
			if(v[i-1]<v[i])
				return false;
		}
		return true;
	}
	/**
	 * Inverses the entire given vector
	 * @param v The vector to inverse
	 */
	private void inverse(int[] v){
		int n = v.length-1,i,temp;
		for(i=0;i<=n/2;i++){
			temp=v[i];
			v[i]=v[n-i];
			v[n-i]=temp;
		}
	}
}